package com.hethongviet.preventingmachine.helps;

import java.util.ArrayList;
import java.util.List;

import com.hethongviet.preventingmachine.models.Tab;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class TabMenuMultiLine
{
	private int _buttonResource = -1;
	private int _buttonResourceHighlight = -1;
	private boolean[] _isActiveButtons = null;
	private Tab[] _arrayTabs = null;
	private int _numberButtons = 0;
	private int _currentButtonActive = 0;

	private List<View> _listLayoutLine = null;

	public TabMenuMultiLine()
	{
		_listLayoutLine = new ArrayList<View>();
	}

	/**
	 * 
	 * @param arrayButtons
	 *            is not null
	 * @param buttonResource
	 * @param buttonResourceHighlight
	 */
	public TabMenuMultiLine(Tab[] arrayTabs, int buttonResource,
			int buttonResourceHighlight, List<View> layoutLine)
	{
		_listLayoutLine = layoutLine;

		_buttonResource = buttonResource;
		_buttonResourceHighlight = buttonResourceHighlight;
		_arrayTabs = arrayTabs;

		_isActiveButtons = new boolean[_numberButtons = arrayTabs.length];
		_isActiveButtons[0] = true;

		for (int i = 0; i < _numberButtons; i++)
		{
			if (i != 0) _isActiveButtons[i] = false;
			if (!arrayTabs[i].isImplementEventAfter())
			{
				final int i1 = i;
				final Button button = arrayTabs[i1].getButton();
				button.setOnClickListener(new OnClickListener() {
					public void onClick(View arg0)
					{
						updateTabLine(button);
						moveToTab(i1);
					}
				});
			}
		}
	}

	private void updateTabLine(Button button)
	{
		int totalLines = _listLayoutLine.size();
		boolean isMatched = false;
		
		int oneTopPadding = 0;
		
		if (totalLines > 1)
		{
			oneTopPadding = _listLayoutLine.get(1).getPaddingTop();
		}
		int indexMatched = 0;
		for (int indexLine = 0; indexLine < totalLines; indexLine++)
		{
			View lineView = _listLayoutLine.get(indexLine);
			
			int currentTopMargin = lineView.getPaddingTop();
			int currentBottomMargin = lineView.getPaddingBottom();
			int currentLeftMargin = lineView.getPaddingLeft();
			int currentRightMargin = lineView.getPaddingRight();
			
			if (isMatched)
			{
				lineView.setPadding(currentLeftMargin, currentTopMargin
						- oneTopPadding, currentRightMargin,
						currentBottomMargin);
			}
			
			else if (lineView.findViewById(button.getId()) != null)
			{
				lineView.setPadding(currentLeftMargin,
						(totalLines - indexLine - 1) * oneTopPadding
								+ currentTopMargin, currentRightMargin,
						currentBottomMargin);
				indexMatched = indexLine;
				isMatched = true;
			}

		}
		
		_listLayoutLine.add(_listLayoutLine.remove(indexMatched));
	}

	public int getCurrentButtonActive()
	{
		return _currentButtonActive;
	}

	public void moveToTab(int tabIndex)
	{
		if (_currentButtonActive != tabIndex)
		{
			/* Reset the old button */
			_isActiveButtons[_currentButtonActive] = false;
			_arrayTabs[_currentButtonActive].getButton().setBackgroundResource(
					_buttonResource);
			// _arrayTabs[_currentButtonActive].getButton().setTextSize(13);
			_arrayTabs[_currentButtonActive].getViewContain().setVisibility(
					View.GONE);
			/* Reset current tab */
			_currentButtonActive = tabIndex;
			/* Update the new button */
			_isActiveButtons[_currentButtonActive] = true;
			_arrayTabs[_currentButtonActive].getButton().setBackgroundResource(
					_buttonResourceHighlight);
			// _arrayTabs[_currentButtonActive].getButton().setTextSize(14);
			_arrayTabs[_currentButtonActive].getViewContain().setVisibility(
					View.VISIBLE);
		}
	}

}
